/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.sigawe.gateway.core;

import java.util.logging.Level;
import java.util.logging.Logger;
import net.sigawe.gateway.Properties.Loader;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

/**
 *
 * @author Rahman
 */
public class Machine {

    XMPPConnection connection;
//    String HOST_SERVER  = "localhost";
//    Integer PORT        = 5222;
//    String USER         = "4321";
//    String PASSWRD      = "4321";
    String HOST_SERVER = null;
    Integer PORT = null;
    String USER = null;
    String PASSWRD = null;
    Loader loader = new Loader();

    public Machine() {
        try {
            HOST_SERVER = loader.getFile().getProperty("SERVER_NAME");
            PORT = Integer.parseInt(loader.getFile().getProperty("SERVER_PORT"));
            USER = loader.getFile().getProperty("USER_NAME_GATEWAY");
            PASSWRD = loader.getFile().getProperty("PASSWORD_GATEWAY");
            listenServer();
        } catch (XMPPException ex) {
            try {
                System.out.println("Tidak terkoneksi dengan "+HOST_SERVER+" ");
                System.out.println("Mencoba koneksi kembali dengan "+HOST_SERVER+"");
                Thread.sleep(18000);
                new Machine();
            //Logger.getLogger(Machine.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InterruptedException ex1) {
                Logger.getLogger(Machine.class.getName()).log(Level.SEVERE, null, ex1);
            }
        //Logger.getLogger(Machine.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void listenServer() throws XMPPException {
        ConnectionConfiguration config = new ConnectionConfiguration(HOST_SERVER, PORT);
        connection = new XMPPConnection(config);
        System.out.println("========== KONFIGURASI ==============");
        System.out.println("Connect to Server   : " + HOST_SERVER);
        System.out.println("PORT Connection     : " + PORT);
        System.out.println("USER GATEWAY        : " + USER);
        System.out.println("PASSWORD GATEWAY    : " + PASSWRD);
        System.out.println("========== KONFIGURASI ==============");
        connection.connect();
        connection.login(USER, PASSWRD);
        System.out.println("======KONEKSI DAN LOGIN SUKSES=======");
        new Receiver(connection);
        if (connection.isConnected()) {
            new Thread(new Sender(connection)).start();
        }

    }
}
